<?PHP
if (!session_start()) { session_start(); };
/*if (!isset($_SESSION['lgnapproved'])) {
	die(header('Location: login.php'));
}*/
include("functions.php");
include("cnnxn.php");
connect();

$SQLev='SELECT * FROM av_events WHERE id='.$_GET['eid'].';';
$sendev=mysqli_query($db,$SQLev) or die($db);
$RSev=mysqli_fetch_object($sendev);

$SQLdiv='SELECT * FROM av_eventdivisions WHERE eventid='.$_GET['eid'].';';
$senddiv=mysqli_query($db,$SQLdiv) or die($db);

$SQLd='SELECT * FROM av_divisions;';
$sendit_d=mysqli_query($db,$SQLd) or die(mysqli_error($db));

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>daleclark.net</title>
<link type="text/css" rel="stylesheet" href="adminstyles.css" />
<script type="text/javascript" language="javascript">
function showhide(divid) {
	if (document.getElementById(divid).style.display=="none") {
		document.getElementById(divid).style.display="block";
	} else {
		document.getElementById(divid).style.display="none";
	}
}

</script>

</head>
<body>
<div id="adminwrapper">
<div id="adminlogo"></div>
<?PHP navigation(); ?>
<div id="admincontent">
<h2><?PHP print $RSev->eventdate; ?></h2>

<div style="float:left; width:500px; padding-right:25px; margin-right:25px; border-right:1px solid #FFFFFF;">

<?PHP 
$backgroundcolor="#2883D4";
while ($RSdiv=mysqli_fetch_object($senddiv)) {
	if ($backgroundcolor=="#2883D4") {
		$backgroundcolor="#2F93E9";
	} else {
		$backgroundcolor="#2883D4";
	}
	$SQLdn='SELECT * FROM av_divisions WHERE id='.$RSdiv->divisionid.';';
	$senddn=mysqli_query($db,$SQLdn) or die(mysqli_error($db));
	$RSdn=mysqli_fetch_object($senddn);
	
	print '<div style="background:'.$backgroundcolor.'; width:500px; padding:2px 2px 2px 5px;"><span class="deletespan"><a href="av_events_removedivision.php?eid='.$RSdiv->eventid.'&did='.$RSdiv->divisionid.'" class="deletelink" title="DELETE" onclick="return confirm(\'Are sure you want to PERMANENTLY DELETE this Division and associated teams from this Event?\')"><img src="../images/icon_delete.png" height="12px" border="0" /></a></span><a href="Javascript:showhide(\''.$RSdn->id.'\')">'.$RSdiv->gender.' - '.$RSdn->divisionname.' - '.$RSdiv->numberofteams.' (CLICK HERE TO ENTER TEAMS)</a><div class="clear"></div></div>';
	
	$SQLpl='SELECT * FROM av_eventplayers WHERE eventid='.$_GET['eid'].' AND divisionid='.$RSdiv->divisionid.' ORDER BY finishposition ASC;';
	$sendpl=mysqli_query($db,$SQLpl) or die($db);
	
	if (mysqli_num_rows($sendpl)>=1) {
		mysqli_data_seek($sendpl,0);
		print '<div id="'.$RSdn->id.'" style="display:none; margin:0px 0px 25px 15px;">
		<table class="playerslist">
		<tr><td colspan="2">PLAYERS</td><td>FINISH</td></tr>';
		while ($RSpl=mysqli_fetch_object($sendpl)) {
			if ($RSdn->teamsize==2) {
				print '<tr><td><a href="av_events_removeteam.php?eid='.$_GET['eid'].'&did='.$RSpl->divisionid.'&tid='.$RSpl->id.'" class="deletelink" title="DELETE" onclick="return confirm(\'Are sure you want to PERMANENTLY DELETE Team from this Event?\')"><img src="../images/icon_delete.png" height="12px" border="0" /></a></td><td style="text-align:left; padding-right:10px;">'.$RSpl->player01.' <span class="lightgray">&amp;</span> '.$RSpl->player02.'</td><td>'.$RSpl->finishposition.'</td></tr>';
			} else {
				print '<tr><td><a href="av_events_removeteam.php?eid='.$_GET['eid'].'&did='.$RSpl->divisionid.'&tid='.$RSpl->id.'" class="deletelink" title="DELETE" onclick="return confirm(\'Are sure you want to PERMANENTLY DELETE Team from this Event?\')"><img src="../images/icon_delete.png" height="12px" border="0" /></a></td><td style="text-align:left; padding-right:10px;">'.$RSpl->player01.' <span class="lightgray">&amp;</span> '.$RSpl->player02.' <span class="lightgray">&amp;</span> '.$RSpl->player03.' <span class="lightgray">&amp;</span> '.$RSpl->player04.'</td><td>'.$RSpl->finishposition.'</td></tr>';
			}
		}
		print '</table>
		</div>';
	} else {
		if ($RSdiv->numberofteams>=1) {
			if ($RSdn->teamsize==2) {
				print '<div id="'.$RSdn->id.'" style="display:none; margin-bottom:15px;">
				<form method="post" action="saveresults.php?eid='.$_GET['eid'].'&did='.$RSdn->id.'">
				<table class="playerslist">
				<tr><td colspan="2">PLAYERS</td><td>FINISH</td></tr>';
				$ti=1;
				while ($ti<=$RSdiv->numberofteams) {
					$ti++;
					print '<tr><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_player01_'.$ti.'" class="shortinput" /></td><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_player02_'.$ti.'" class="shortinput" /></td><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_finishposition_'.$ti.'" class="shortestinput" /></td></tr>';
				}
				print '</table>
				<input type="submit" name="submit" value=" Submit Results " />
				</form>
				</div>';
			} else {
				print '<div id="'.$RSdn->id.'" style="display:none; margin-bottom:15px;">
				<form method="post" action="saveresults.php?eid='.$_GET['eid'].'&did='.$RSdn->id.'">
				<table class="playerslist">
				<tr><td colspan="4" >PLAYERS</td><td>FINISH</td></tr>';
				$ti=1;
				while ($ti<=$RSdiv->numberofteams) {
					$ti++;
					print '<tr><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_player01_'.$ti.'" class="shortinput" /></td><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_player02_'.$ti.'" class="shortinput" /></td><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_player03_'.$ti.'" class="shortinput" /></td><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_player04_'.$ti.'" class="shortinput" /></td><td><input type="text" name="'.$_GET['eid'].'_'.$RSdiv->id.'_finishposition_'.$ti.'" class="shortestinput" /></td></tr>';
				}
				print '</table>
				<input type="submit" name="submit" value=" Submit Results " />
				</form>
				</div>';
			}
		}
	}
	
}
?>
<div class="clear"></div>
<p>&nbsp;</p>
</div>
<div style="float:left; width:500px;">

<h3>Add a Division for this Event</h3>
<form name="addForm" method="POST" action="av_eventdivisions_add.php">
<input name="eventid" type="hidden" value="<?PHP print $_GET['eid']; ?>" />
<p><span class="label">Gender</span><span class="inputbox"><select name="gender">
<option value="M" selected>M</option>
<option value="F">F</option>
<option value="Co">Co</option>
</select></span></p>
<p><span class="label">Division</span><span class="inputbox"><select name="divisionid">
<?PHP
while ($RSd=mysqli_fetch_object($sendit_d)) {
	print '<option value="'.$RSd->id.'">'.$RSd->divisionname.'</option>';
}
?>
</select></span></p>
<p><span class="label">Number of Teams</span><span class="inputbox"><input name="numberofteams" type="text" style="width:50px;" /></span></p>
<input type="submit" value="submit" name="submit" /></form>

</div>
</div>
</div>
</body>
</html>
<?PHP
mysqli_close($db);
?>
<?PHP
mysqli_close($db);
?>
